#if !defined(_DBFMEMCURSORSET_H__INCLUDED_)
#define _DBFMEMCURSORSET_H__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "DBFRecordset.h"

class TDBFUTILITY_IMPEXP CDBFMemCursorset : public CDBFRecordset
{
	DECLARE_DYNAMIC(CDBFMemCursorset)

// Constructor
public:
	CDBFMemCursorset(CDBFTableDef* pDBFTable = NULL);

	BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlag = TG_DBF_USE_SHARED);
	void Close();

// Attributes
public:
	BOOL IsOpen() const;
	BOOL IsBOF() const;
	BOOL IsEOF() const;

	BOOL IsDeleted();

	long GetRecordCount();

protected:
	DBF_HEAD	m_DBFHead;

	long m_nCurCursorRecN;
	BOOL m_bBOF;			
	BOOL m_bEOF;

// Operations
public:
	// Cursor operations
	void MoveNext();
	void MovePrev();
	void MoveFirst();
	void MoveLast();
	void Move(long lOffsetRec);


	long GetAbsolutePosition();
	void SetAbsolutePosition(long lPosition);

	// edit buffer operations
	virtual void AddNew();
	virtual void Edit();

	virtual void Update();
	virtual void CancelUpdate();

	virtual void Delete();
	virtual void Undelete();
	virtual void Remove();

	// Recordset operations
	void Requery();
	BOOL Save();

	void SetDelete(BOOL bHideDeleteRec = TRUE);
	BOOL GetDelete();

	void SetFilter(BOOL (FAR * lpfnIsInclude) (DBF_CHAR*, CDBFFields* pDBFFields));

	void SortByField(int nIndex, BOOL bDesc = FALSE);
	void SortByField(const char* lpszName, BOOL bDesc = FALSE);

// Implementation
public:
	virtual ~CDBFMemCursorset();

#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif


protected:
	virtual void ThrowDBFException(long nError = NO_TG_DBF_ERROR);

private:

	DBF_CHAR* GetRecord(long lRecNumber);
	void GetRecord(long lRecNumber, DBF_CHAR* lpBuffer);
	void SetRecord(long lRecNumber, const DBF_CHAR* lpBuffer);

	void qsort (int from, int to);
	int compare(const void *elem1, const void *elem2 );

	DBF_UCHAR*	m_pTableData;
	CPtrArray	m_RecPtr;
	CPtrArray	m_NewRecPtr;
	int			m_nSortedField;
	BOOL		m_bDesc;
	BOOL		m_bShowDeletedRec;
	DBF_CHAR*	m_pEditRec;
	DBF_LONG	m_lLoadRecord;

	BOOL (FAR * m_lpfnIsInclude) (DBF_CHAR*, CDBFFields* pDBFFields);
};

//{{AFX_INSERT_LOCATION}}
#endif // !defined(_DBFMEMCURSORSET_H__INCLUDED_)
